package com.sys.common.exception;

import com.sys.common.code.ResultEnum;
import com.sys.common.util.ResponseDateUtil;
import com.sys.common.util.ResultBuilder;
import org.apache.shiro.authc.ExpiredCredentialsException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局配置shiro异常处理
 */
@ControllerAdvice
public class ShiroException {

    /**
     * 权限不足
     *
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(UnauthorizedException.class)
    @ResponseBody
    public ResponseDateUtil defaultExceptionHandler(HttpServletRequest req, Exception e) {
        return ResultBuilder.errorInfo(ResultEnum.FORBIDDEN);
    }

    /**
     * 登录过期
     *
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(ExpiredCredentialsException.class)
    @ResponseBody
    public ResponseDateUtil logTimeOut(HttpServletRequest req, Exception e) {
        return ResultBuilder.errorInfo(ResultEnum.AUTHORIZEDOUT);
    }
}
